package com.goatrock.media.thisway.domain.service.impl;

import com.goatrock.media.thisway.common.exceptions.ServiceException;
import com.goatrock.media.thisway.common.model.ApplicationConfig;
import com.goatrock.media.thisway.common.model.ShortcutRequest;
import com.goatrock.media.thisway.common.model.Title;
import com.goatrock.media.thisway.domain.adapter.ShortcutServiceAdapter;
import com.goatrock.media.thisway.domain.filter.TitleFilter;
import com.goatrock.media.thisway.domain.service.ShortcutService;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;

public class ShortcutServiceImpl implements ShortcutService {
    ShortcutServiceAdapter shortcutAdapter;
    private Logger logger = Logger.getLogger(ShortcutServiceImpl.class);
    private Logger loggerMetrics = Logger.getLogger("METRICS");
    private List<TitleFilter> filters;

    public void GenerateShortcutsByParameters(ShortcutRequest shortcutParams, ApplicationConfig appConfig) throws ServiceException{
        logger.debug("Inside ShortcutService");
        List<Title> titles = shortcutParams.getTitles();
        if(titles != null){
            final long startTime = System.currentTimeMillis();

            Iterator j = filters.iterator();
            while(j.hasNext()){
                TitleFilter filter = (TitleFilter)j.next();

                if(filter.isExecutable(shortcutParams.getParameters())){
                    logger.debug("*********** Beginning filter:"+filter.getName()+ " ***********");
                    removeExistingShortcutFolder(appConfig,filter.getFolderName());

                    Iterator i = titles.iterator();
                    while(i.hasNext()){
                        Title title = (Title)i.next();

                        String[] paths = filter.execute(shortcutParams.getParameters(), title);

                        if(paths != null && paths.length > 0){
                            for(int p=0;p<paths.length;p++){
                                logger.debug(title);
                                logger.debug("Creating shortcut for: "+paths[p]);
                                shortcutAdapter.GenerateShortcut(appConfig,paths[p], title.getOriginalTitle(), title.getBaseFileLocation());
                            }
                        }
                    }
                    logger.debug("*********** Complete filter:"+filter.getName()+" ***********");
                }else{
                    logger.debug("Filter - "+filter.getName()+ " - does not apply.");
                }
            }

            final long endTime = System.currentTimeMillis();
            final long totalTime = endTime - startTime;

            loggerMetrics.info("ShortcutCreation time/number titles: "+totalTime+"/"+titles.size());
        }
    }

    public void GenerateShortcutsByField(String field, String shortcutLocation) {
        //TO-DO: implement this method; as of right now, it's not needed
    }

    public ShortcutServiceAdapter getShortcutAdapter() {
        return shortcutAdapter;
    }

    public void setShortcutAdapter(ShortcutServiceAdapter shortcutAdapter) {
        this.shortcutAdapter = shortcutAdapter;
    }

    public List<TitleFilter> getFilters() {
        return filters;
    }

    public void setFilters(List<TitleFilter> filters) {
        this.filters = filters;
    }

    private void removeExistingShortcutFolder(ApplicationConfig appConfig, String subFolder) {
        try {
            shortcutAdapter.RemoveShortcutFolder(appConfig,subFolder);
        } catch (ServiceException ex) {
            logger.error("Error while removing existing shortcut folder. ErrorCode:"+ex.getReturnMessage());
        }
    }

}
